#! /bin/bash

pid=$*

#通过配置文件读取端口号
conf_file=`ps aux ww|grep $pid|grep -v grep|awk '{for(i=1;i<=NF;++i){if(index($i,".cfg")>1)print $i}}'|head -1`
if [ -n "$conf_file" ];then
   port=`sed '/clientPort/!d;s/.*=//' $conf_file|tr -d '\r'`
   echo $port
else
	#通过 netstat 读取端口号
	ports=`netstat -lntp|grep $pid|awk '{print $4}'|awk -F ':' '{print $NF}'`
	p_number=`echo $ports|awk '{print NF}'`
	if [ "$p_number" -gt 1 ];then
	        echo $ports|awk '{if($1>$2){print $2}else{print $1}}'
	else
	        echo $ports
	fi
fi

